package com.bdqn.controller;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bdqn.entity.PsInspectiontaskDetail;
import com.bdqn.entity.PsInspectiontaskMain;
import com.bdqn.entity.PsInspectorDetail;
import com.bdqn.entity.PsLineInfo;
import com.bdqn.entity.PsPoleInfo;
import com.bdqn.entity.PsSystemconfig;
import com.bdqn.entity.PsUser;
import com.bdqn.service.IPsInspectiontaskDetailService;
import com.bdqn.service.IPsInspectiontaskMainService;
import com.bdqn.service.IPsInspectorDetailService;
import com.bdqn.service.IPsLineInfoService;
import com.bdqn.service.IPsPoleInfoService;
import com.bdqn.service.IPsSystemconfigService;
import com.bdqn.service.IPsUserService;
/**
 * 
 * @author 德德
 *
 */
@RestController
public class HomeController {
	
	@Autowired
	IPsUserService ipus;
	@Autowired
	IPsPoleInfoService poleInfo;
	@Autowired
	IPsLineInfoService lineInfo;
	@Autowired
	IPsInspectiontaskMainService inspectiontaskMain;
	@Autowired
	IPsSystemconfigService systemconfig;
	@Autowired
	IPsInspectorDetailService inspectorDetailServicel;
	@Autowired
	IPsInspectiontaskDetailService inspectiontaskDetail;
	@Autowired
	private IPsUserService psUserService;
	
	
	//登录
	@RequestMapping("login")
	public PsUser login(@RequestParam(name="name",required=false)String name,@RequestParam(name="pwd",required=false)String pwd) {
		System.out.println(name+","+pwd);
		PsUser puser=ipus.checkLogin(name,pwd);
		if(puser!=null) {
			return puser;
		}else {
			System.out.println("kong");
			return null;
		}
	}	
	//根据id查用户
	@RequestMapping("subi")
	public PsUser selectUserById(@RequestParam(name="id",required=false)Integer id) {
		return ipus.getById(id);
	}
	
	//个人更改数据
	@RequestMapping("myUserData")
	public boolean myuserdata(@RequestParam(name="id",required=false)Integer id,
							@RequestParam(name="age",required=false)Integer age,
							@RequestParam(name="pwd",required=false)String pwd,
							@RequestParam(name="sex",required=false)String sex,
							@RequestParam(name="phone",required=false)String phonetel,
							@RequestParam(name="email",required=false)String email) {
		//System.out.println(id+""+age+""+sex);
		//修改数据
		PsUser user=ipus.getById(id);
		user.setAge(age);
		user.setUserpassword(pwd);
		user.setSex(sex);
		user.setPhonetel(phonetel);
		user.setEmail(email);
		//ipus.updateById(user);
		
		return ipus.updateById(user);
	}
	
	//查询线路
	@RequestMapping("getAllLine")
	public List<PsLineInfo> selectAllLine() {
		
		return lineInfo.list();
	}
	//查询杆塔
	@RequestMapping("getAllPole")
	public List<PsPoleInfo> selectAllPole() {
		return poleInfo.list();
	}
	//根据id查线路
	@RequestMapping("selectPsLineInfoById")
	public PsLineInfo selectPsLineInfoById(@RequestParam(name="id",required=false)Integer id) {
		return lineInfo.getById(id);
	}
	//根据线路id查杆塔
	@RequestMapping("selectPoleByLineId")
	public List<PsPoleInfo> selectPoleByLineId(@RequestParam(name="lid",required=false)Integer lid) {
		
		
		return poleInfo.selectPoleByLineId(lid);
	}
	//根据id删除杆塔
	@RequestMapping("delPoleById")
	public boolean delPoleById(@RequestParam(name="id",required=false)Integer id) {
		return poleInfo.removeById(id);
	}
	//根据id查杆塔
	@RequestMapping("selectPoleById")
	public PsPoleInfo selectPoleById(@RequestParam(name="id",required=false)Integer id) {
		return poleInfo.getById(id);
	}
	//修改杆塔
	@RequestMapping("upDataPoleById")
	public boolean upDataPoleById(@RequestParam(name="id",required=false)Integer id,
								@RequestParam(name="isstart",required=false)Integer isstart) {
		PsPoleInfo ploe=poleInfo.getById(id);
		ploe.setIsstart(isstart);
		return poleInfo.updateById(ploe);
	}
	//新增杆塔
	@RequestMapping("addPole")
	public boolean addPole(@RequestParam(name="polecode",required=false)String polecode,
							@RequestParam(name="lineid",required=false)Long lineid,
							@RequestParam(name="creattime",required=false)Long creattime,
							@RequestParam(name="creatby",required=false)String creatby,
							@RequestParam(name="iss",required=false)Integer iss) {
		System.out.println(polecode+","+lineid+","+creattime+","+creatby+","+iss);
		//把long格式转换
		LocalDateTime time =LocalDateTime.ofEpochSecond(creattime/1000,0,ZoneOffset.ofHours(8));
		//创建杆塔对象
		PsPoleInfo ploe=new PsPoleInfo();
		//设置数据
		ploe.setPolecode(polecode);
		ploe.setLineid(lineid);
		ploe.setCreationtime(time);
		ploe.setCreatedby(creatby);
		ploe.setIsstart(iss);

		return poleInfo.save(ploe);
	}
	
	//查询所有巡查任务
	@RequestMapping("selectAllInspection")
	public List<PsInspectiontaskMain> selectAllInspection(){
		return inspectiontaskMain.list();		
	}
	//根据id查巡查任务
	@RequestMapping("selectInspectionByid")
	public PsInspectiontaskMain selectInspectionByid(@RequestParam(name="id",required=false)Integer id){
		System.out.println(id);
		return inspectiontaskMain.getById(id);		
	}
	//根据id查任务状态表
	@RequestMapping("selectSystemconfigByid")
	public PsSystemconfig selectSystemconfigByid(@RequestParam(name="id",required=false)Integer id) {
		return systemconfig.getById(id);
	}
	//根据完成状态查任务主表
	@RequestMapping("selectMainByTaskId")
	public List<PsInspectiontaskMain> selectMainByTaskId() {
		
		return inspectiontaskMain.selectMainByTaskId();
	}
	//根据任务id查执行人
	@RequestMapping("selectDetailByMainId")
	public List<PsInspectorDetail> selectDetailByMainId(@RequestParam(name="id",required=false)Integer id){
		
		return inspectorDetailServicel.selectDetailByMainId(id);
	}
	//根据主表id查子表
	@RequestMapping("selectInsDetailByMainId")
	public PsInspectiontaskDetail selectInsDetailByMainId(@RequestParam(name="id",required=false)Integer id) {
		return inspectiontaskDetail.selectInsDetailByMainId(id);
	}
	//巡查任务updata
	@RequestMapping("upInspection")
	public boolean upInspection(@RequestParam(name="id",required=false)Integer id,
								@RequestParam(name="inscode",required=false)String inspectiontaskcode,
								@RequestParam(name="insname",required=false)String inspectiontaskname,
								@RequestParam(name="startpole",required=false)Long startpole,
								@RequestParam(name="endpole",required=false)Long endpole,
								@RequestParam(name="taskStatusId",required=false)Long taskStatusId,
								@RequestParam(name="iscancel",required=false)Integer iscancel) {
		
		System.out.println(id+"."+inspectiontaskcode+"."+inspectiontaskname+"."+startpole+"."+endpole+"."+taskStatusId+"."+iscancel);
		//根据ID查出对应的任务
		PsInspectiontaskMain pim=inspectiontaskMain.getById(id);
		//设置数据
		pim.setInspectiontaskcode(inspectiontaskcode);
		pim.setInspectiontaskname(inspectiontaskname);
		pim.setStartpole(startpole);
		pim.setEndpole(endpole);
		pim.setTaskStatusId(taskStatusId+17);
		pim.setIscancel(iscancel);
		
		Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
		LocalDateTime lastupdatetime=instant.atZone(zoneId).toLocalDateTime();
		pim.setLastupdatetime(lastupdatetime);;
		
		return inspectiontaskMain.updateById(pim);
	}
	
	//查出所有巡查员
	@RequestMapping("selectRole")
	public List<PsUser> selectRole(){
		return psUserService.selectRole();
	}
	//分配任务
	@RequestMapping("addInspector")
	public boolean addInspector(@RequestParam(name="user",required=false)String user[],
								@RequestParam(name="id",required=false)Long id,
								@RequestParam(name="id",required=false)Integer uid) {
		String str="";
		for(int i=0;i<user.length;i++) {
			str=str+user[i].substring(user[i].indexOf("\"")+1);
			System.out.println(str);
		}
		System.out.println();
		String [] astr=str.split("\"");
		boolean b=true;
		for(int i=0;i<astr.length-1;i++) {
			System.out.println(astr[i]);
			PsUser u=psUserService.selectCode(astr[i]);
			PsUser idu=psUserService.getById(uid);
			PsInspectorDetail pid=new PsInspectorDetail();
			pid.setTaskid(id);
			pid.setInspectorcode(u.getUsercode());
			pid.setInspectorname(u.getUsername());
			pid.setCreatedby(idu.getUsercode());
			
			Date date = new Date();
	        Instant instant = date.toInstant();
	        ZoneId zoneId = ZoneId.systemDefault();
			LocalDateTime lastupdatetime=instant.atZone(zoneId).toLocalDateTime();
			pid.setCreationtime(lastupdatetime);
			b=inspectorDetailServicel.save(pid);
			//System.out.println("ok");
		}
		return b;
	}
	//查执行中的任务子表
	@RequestMapping("selectRequest")
	public List<PsInspectiontaskDetail> selectRequest(){
		return inspectiontaskDetail.selectRequest();
	}
	//根据id查询子表
	@RequestMapping("selectDetailById")
	public PsInspectiontaskDetail selectDetailById(@RequestParam(name="id",required=false)Integer id) {
		return inspectiontaskDetail.selectDetailById(id);
	}
	//添加回执
	@RequestMapping("receiptAdd")
	public boolean receiptAdd(@RequestParam(name="taskid",required=false)Integer taskid,
							@RequestParam(name="poleid",required=false)Long poleid,
							@RequestParam(name="intactRate",required=false)Double intactRate,
							@RequestParam(name="bugLevelId",required=false)Long bugLevelId,
							@RequestParam(name="bugTypeId",required=false)Long bugTypeId,
							@RequestParam(name="bugdesc",required=false)String bugdesc,
							@RequestParam(name="uid",required=false)Integer uid,
							@RequestParam(name="distime",required=false)String distime) {
		System.out.println(taskid);
		System.out.println(poleid);
		System.out.println(intactRate);
		System.out.println(bugLevelId);
		System.out.println(bugTypeId);
		System.out.println(bugdesc);
		System.out.println(uid);
		System.out.println(distime);
		
		PsInspectiontaskDetail pitd=new PsInspectiontaskDetail();
		PsUser user=ipus.getById(uid);
//		
//		pitd.setPsInspectiontaskMain(inspectiontaskMain.getById(taskid));
//		pitd.setTaskstatus(17L);
//		pitd.setPoleid(poleid);
//		pitd.setIsbug(1);
//		pitd.setIntactrate(intactRate);
//		pitd.setBugLevelId(bugLevelId+3);
//		pitd.setBugTypeId(bugTypeId+6);
//		pitd.setBugdesc(bugdesc);
//		
//		pitd.setDiscoverercode(user.getUsercode());
//		pitd.setDiscoverername(user.getUsername());
//		pitd.setIssolve(0);
//		pitd.setIsbuglevel(0);
//		pitd.setCreatedby("admin");
//		
		Date da = new Date();
        Instant ins = da.toInstant();
        ZoneId zone = ZoneId.systemDefault();
		LocalDateTime ltime=ins.atZone(zone).toLocalDateTime();
//		pitd.setInspecttime(ltime);
//		pitd.setCreationtime(ltime);
//		pitd.setLastupdatetime(ltime);
		
		//处理时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
            Date date = sdf.parse(distime);
            Instant instant = date.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
    		LocalDateTime lastupdatetime=instant.atZone(zoneId).toLocalDateTime();
    		pitd.setDiscovertime(lastupdatetime);
        } catch (Exception e) {
            e.printStackTrace();
        }
		//System.out.println(pitd);
		return inspectiontaskDetail.add(taskid,17L,poleid,1,intactRate,bugLevelId+3,bugTypeId+6,bugdesc,user.getUsercode(),
				user.getUsername(),0,0,"admin",ltime,ltime,ltime,pitd.getDiscovertime());
	}
}
